import {ref} from 'vue'
import { ElMessage } from 'element-plus'
import { fa } from 'element-plus/es/locale/index.mjs'
export function useDevice(){
    const hasAudioInput=ref(false)
    const hasVideoInput=ref(false)
    const hasScreenInput=ref(false)
    const videoRef=ref()
    const localeStream={
         videoStream:null,
         audioStream:null,
         screenStream:null

    }
   
    const tip=ref('')

   async function initDevice(){
    //获取系统里面枚举的设备的列表
        const devices = await navigator.mediaDevices.enumerateDevices()
        const audioInput=devices.find((item)=>(item.kind==='audioinput'))
        const videoInput=devices.find((item)=>(item.kind==='videoinput'))
        const audioOutput=devices.find((item)=>(item.kind==='audiooutput'))
        hasVideoInput.value=!!videoInput?.deviceId
        hasAudioInput.value=!!audioInput?.deviceId
        if(hasVideoInput.value){
          await  openVideo()
        }else{
            tip.value='摄像头处于关闭状态';
        }
    }
  
    async function openMicro(){
        try{
            localeStream.audioStream=await navigator.mediaDevices.getUserMedia({audio:true})
            hasAudioInput.value=true
            
        }catch(e){
            let message =''
            if(['NotAllowedError','PermissionDeniedError'].includes(e.name)) {
                message ='用户拒绝授予麦克风权限'
                   
            }else if(['NotFoundError','DevicesNotFoundError'].includes(e.name)){
                message ='没有找到可用的麦克风设备'
             
            }else if('evice in use'.includes(e.name)){
               message='麦克风被其他程序占用'
             
            }   
            ElMessage.error(message||'麦克风开启出错')
        }
       
    }
    function closeMicro(){
        hasAudioInput.value=false
       
        const audioTracks= localeStream.audioStream?.getTracks()
        audioTracks?.forEach((track)=>track.stop())
        tip.value='摄像头处于关闭状态'
    }

    async function openVideo(){
        try{
            tip.value='摄像头开启中'
            localeStream.videoStream=await navigator.mediaDevices.getUserMedia({video:true})
            videoRef.value.srcObject=localeStream.videoStream
            hasVideoInput.value=true
        }catch(e){
            let message =''
            if(['NotAllowedError','PermissionDeniedError'].includes(e.name)) {
                message ='用户拒绝授子摄像头权限'
                     tip.value='用户拒绝授子摄像头权限'
            }else if(['NotFoundError','DevicesNotFoundError'].includes(e.name)){
                message ='没有找到可用的摄像头设备'
                tip.value='没有找到可用的摄像头设备'
            }else if('evice in use'.includes(e.name)){
               message='摄像头被其他程序占用'
               tip.value='摄像头被其他程序占用'
            }   
            ElMessage.error(message||'摄像头开启出错')
             
        }
     
    }
    function closeVideo(){
        hasVideoInput.value=false
        videoRef.value.srcObject=null
        const videoTracks=localeStream.videoStream?.getTracks()
        videoTracks?.forEach((track)=>track.stop())
       
        tip.value='摄像头处于关闭状态'

    }

   async function openScreen(){
       const screenStream=await navigator.mediaDevices.getDisplayMedia({video:true ,audio:false})
       const oldScreenTracks=localeStream.screenStream?.getTracks()
       oldScreenTracks?.forEach((track)=>track.stop())
        
       videoRef.value.srcObject=screenStream
       localeStream.screenStream=screenStream
       hasVideoInput.value=false
        hasScreenInput.value=true
       const videoTracks=localeStream.videoStream?.getTracks()
       videoTracks?.forEach((track)=>track.stop())
       tip.value=''
    }
    function closeScreen(){

    }
    return {
        openMicro,
        closeMicro,
        openVideo,
        closeVideo,
        hasVideoInput,
        hasAudioInput,
        videoRef,
        tip,
        openScreen,
        closeScreen,
        localeStream,
        initDevice
    }
}